package evq.codec.ui.util;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;

import evq.codec.core.entities.Pgm;
import evq.codec.core.entities.Vector;
import evq.codec.core.managers.impl.DefaultFileManager;

public class ImageUtil {

	DefaultFileManager fileManager = DefaultFileManager.getInstance();

	public BufferedImage pgmToJpg(String pgmFilepath) throws IOException {
		BufferedImage image = null;
		List<Integer> imgList = new ArrayList<Integer>();

		Pgm pgm = this.fileManager.loadPgmFile(pgmFilepath);

		for (List<Integer> list : pgm.getImage()) {
			imgList.addAll(list);
		}

		int width = pgm.getWidth();
		int height = pgm.getHeight();

		Integer[] myImage = imgList.toArray(new Integer[imgList.size()]);

		BufferedImage im = new BufferedImage(width, height,
				BufferedImage.TYPE_BYTE_GRAY);
		WritableRaster raster = im.getRaster();
		for (int h = 0; h < height; h++) {
			for (int w = 0; w < width; w++) {
				raster.setSample(w, h, 0, myImage[h * width + w]);
			}
		}

		ByteArrayOutputStream myJpg = new ByteArrayOutputStream();

		ImageIO.write(im, "jpg", myJpg);

		ByteArrayInputStream input = new ByteArrayInputStream(myJpg
				.toByteArray());

		image = ImageIO.read(input);

		return image;
	}

	public BufferedImage dicToJpg(Vector dictionary, int dimension)
			throws IOException {
		List<Integer> imgList = new ArrayList<Integer>();

		for (Iterator iterator = dictionary.iterator(); iterator.hasNext();) {
			imgList.add(((Long) Math.round((Double) iterator.next()))
					.intValue());
		}

		int width = (int) Math.sqrt(dimension);
		int height = (int) Math.sqrt(dimension);

		Integer[] myImage = imgList.toArray(new Integer[imgList.size()]);

		BufferedImage im = new BufferedImage(width, height,
				BufferedImage.TYPE_BYTE_GRAY);
		WritableRaster raster = im.getRaster();
		for (int h = 0; h < height; h++) {
			for (int w = 0; w < width; w++) {
				raster.setSample(w, h, 0, myImage[h * width + w]);
			}
		}

		ByteArrayOutputStream myJpg = new ByteArrayOutputStream();

		ImageIO.write(im, "jpg", myJpg);

		ByteArrayInputStream input = new ByteArrayInputStream(myJpg
				.toByteArray());

		BufferedImage image = ImageIO.read(input);

		return image;
	}

	public int getBlock(int x, int y, int dimension, int height) {
		// Tamanho do bloco
		int largurabloco = (int) Math.sqrt(dimension);
		int alturabloco = (int) Math.sqrt(dimension);

		// Bloco horizontal, arredonda para baixo
		int bx = (int) Math.floor(x / largurabloco) + 1;

		// Bloco vertical, arredonda para baixo
		int by = (int) Math.floor(y / alturabloco) + 1;

		// Quantidade de blocos por coluna
		int bv = Math.round(height / alturabloco);

		// Numero de blocos até a coluna anterior
	    int bac = (bx - 1) * bv;
	   
	    // Numero do bloco somando o bloco vertical com o número de blocos das colunas anteriores
	    int bloco = by + bac;

		return bloco;

	}
}
